package utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class Processor
{
	private Processor me;
	
	private HashMap<Task, Integer> taskSchedule; // Contains start for each scheduled task
	private ArrayList<Edge> edgeSchedule;
	private int readyTime;
	private String name;

	public Processor(String name)
	{
		this.me = this;
		
		this.name = name;
		this.taskSchedule = new HashMap<Task, Integer>();
		this.edgeSchedule = new ArrayList<Edge>();
		this.readyTime = 0;
	}
	
	public String toString()
	{
		return name;
	}

	public int getReadyTime()
	{
		return readyTime;
	}
	
	public void scheduleEdge(Edge edge)
	{
		edgeSchedule.add(edge);
	}
	
	public void scheduleTask(Task task) {
		
		if(taskSchedule.containsKey(task))
		{
			throw new RuntimeException("Have that task already.");
		}
		
		readyTime = task.getStartTime(me) + task.getExecutionTime();
		taskSchedule.put(task, task.getStartTime(me));
	}

	public Integer getTaskStartTime(Task task)
	{
		return taskSchedule.get(task);
	}
	
	public String getScheduleAsString()
	{
		String scheduleString = "";
		for (Map.Entry<Task, Integer> entry : taskSchedule.entrySet())
		{
			scheduleString += "Task: " + entry.getKey().getId()
			        + " FinishTime: " + entry.getValue() + "\n";
		}
		return scheduleString;
	}

	public HashMap<Task, Integer> getTaskSchedule()
	{
		return taskSchedule;
	}

	public ArrayList<Edge> getEdgeSchedule()
	{
		return edgeSchedule;
	}


	public String getName()
    {
    	return name;
    }

	public void setName(String name)
    {
    	this.name = name;
    }

}
